Java Grade Calculator program for class kepp geting the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException." [migrated]
Posted
by
user2880621
on Programmers
See other posts from Programmers
or by user2880621
Published on 2013-10-26T01:43:35Z
Indexed on
2013/10/26
4:09 UTC
Read the original article
Hit count: 211
this is my first question I've posted. I've used this site to look at other questions to help with similar problems I've had. After some cursory looking I couldn't find quite the answer I was looking for so I decided to finally succumb and create an account.
I am fairly new to java, only a few weeks into my first class. Anyway, my project is to create a program which takes any amount of students and their grades, and then assign them a letter grade. The catch is, however, that it is on a sort of curve and the other grades' letter are dependent on the the highest. Anything equal to or 10 points below the best grade is an a, anything 11-20 points below is a b, and so on. I am to use an array, but I get this error when ran "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException."
I will go ahead and post my code down below. Thanks for any advice you may be able to give.
package grade.calculator; import java.util.Scanner; /** * * @author nichol57 */ public class GradeCalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of students");
int number = input.nextInt();
double[]grades = new double [number];
for (int J = number; J >=0; J--) {
System.out.println("Enter the students' grades");
grades[J] = input.nextDouble();
}
double best = grades[0];
for (int J = 1; J < number; J++) {
if (grades[J] >= best){
best = grades[J];
}
}
for (int J = 0;J < number; J++){
if (grades[J] >= best - 10){
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "A");
}
else if (grades[J] >= best - 20){
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "B");
}
else if (grades[J] >= best - 30) {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "C");
}
else if (grades[J] >= best - 40) {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "D");
}
else {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "F");
}
} // end for loop for output
}// end main method
}
© Programmers or respective owner